


var quikos = function() { };
quikos.id = {};
quikos.idspec = {};

#if OPENSOCIAL_0_8

quikos.id.VIEWER = "VIEWER";
quikos.id.OWNER = "OWNER";
quikos.idspec.VIEWER = opensocial.newIdSpec(
    {"userId": "VIEWER", "groupId": "SELF" });
quikos.idspec.OWNER = opensocial.newIdSpec(
    {"userId": "OWNER", "groupId": "SELF" });
quikos.idspec.VIEWER_FRIENDS = opensocial.newIdSpec(
    {"userId": "VIEWER", "groupId": "FRIENDS" });
quikos.idspec.OWNER_FRIENDS = opensocial.newIdSpec(
    {"userId": "OWNER", "groupId": "FRIENDS" });
quikos.idspec.personById = function(id) {
  return opensocial.newIdSpec(
    {"userId": id, "groupId": "SELF"});  
};
quikos.idspec.friendsById = function(id) {
  return opensocial.newIdSpec(
    {"userId": id, "groupId": "FRIENDS"});
};

#elif OPENSOCIAL_0_7

quikos.id.VIEWER = "VIEWER";
quikos.id.OWNER = "OWNER";
quikos.idspec.VIEWER = "VIEWER";
quikos.idspec.OWNER = "OWNER";
quikos.idspec.VIEWER_FRIENDS = "VIEWER_FRIENDS";
quikos.idspec.OWNER_FRIENDS = "OWNER_FRIENDS";
quikos.idspec.personById = function(id) {
  return id;
};
quikos.idspec.friendsById = function(id) {
  throw new Error("Friends by ID is not supported in OpenSocial 0.7!");
};

#endif 

quikos.PeopleParams = function() {};

quikos.PeopleParams.prototype.ensureProfileDetails_ = function() {
  if (!this[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS]) {
    this[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [];
  }
};
quikos.PeopleParams.prototype.setFirst = function(first) {
  this[opensocial.DataRequest.PeopleRequestFields.FIRST] = first;
  return this;
};

quikos.PeopleParams.prototype.setMax = function(max) {
  this[opensocial.DataRequest.PeopleRequestFields.MAX] = max;
  return this;
};

quikos.PeopleParams.prototype.addFields = function(fields) {
  this.ensureProfileDetails_();
  if (typeof fields == "string") {
    fields = [ fields ];
  }
  for (var i = 0, field; field = fields[i]; i++) {
    if (opensocial.Person.Field.hasOwnProperty(field)) {
      this[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS]
           .push(opensocial.Person.Field[field]);
    }
  }
  return this;
};


quikos.prototype.ensureDataRequest_ = function() {
  if (!this.request_) { 
    this.tags_ = [];
    this.errors_ = {};
    this.request_ = opensocial.newDataRequest()
  }
};

quikos.prototype.closeHandleResponse_ = function(callback) {
  var me = this;
  return function (data) {
    var mod_data = {};
    for (var i = 0, tag; tag = me.tags_[i]; i++) {
      var resp = data.get(tag);
      if (!resp.hadError()) {
        mod_data[tag] = resp.getData();
      } else {
        mod_data[tag] = null;
        me.errors_[tag] = { 
            "error_message": resp.getErrorMessage(),
            "error_code": resp.getErrorCode()
        };
      }
    };
    callback(mod_data, me)
  }
};

quikos.prototype.getErrorMessage = function(tag) {
  if (this.errors_[tag]) {
    return this.errors_[tag].error_message;
  }
};

quikos.prototype.getErrorCode = function(tag) {
  if (this.errors_[tag]) {
    return this.errors_[tag].error_code;
  }  
};

quikos.prototype.getFullErrorMessage = function(tag) {
  if (this.errors_[tag]) {
    return "[" + this.errors_[tag].error_code + "] " +
           this.errors_[tag].error_message;
  }
};

quikos.prototype.hadError = function(tag) {
  return (this.errors_.hasOwnProperty(tag));
};

quikos.prototype.startRequest = function() {
  this.ensureDataRequest_();
  return this;
};

quikos.prototype.sendRequest = function(callback) {
  this.ensureDataRequest_();
  this.request_.send(this.closeHandleResponse_(callback));
  return this;
};

quikos.prototype.fetchPerson = function(id, tag, opt_params) {
  this.ensureDataRequest_();
  var request = this.request_.newFetchPersonRequest(id, opt_params);
  this.request_.add(request, tag);
  this.tags_.push(tag);
  return this;
};

quikos.prototype.fetchPeople = function(idspec, tag, opt_params) {
  this.ensureDataRequest_();
  var request = this.request_.newFetchPeopleRequest(idspec, opt_params);
  this.request_.add(request, tag);
  this.tags_.push(tag);
  return this;
};

quikos.prototype.fetchData = function(idspec, tag, opt_keys, opt_params) {
  this.ensureDataRequest_();
  if (!opt_keys) {
    opt_keys = "*";
  }
  var request = this.request_.newFetchPersonAppDataRequest(idspec, opt_keys, 
      opt_params);
  this.request_.add(request, tag);
  this.tags_.push(tag);
  return this;
};

quikos.prototype.setData = function(key, value, tag) {
  this.ensureDataRequest_();
  var request = this.request_.newUpdatePersonAppDataRequest(quikos.id.VIEWER,
      key, value);
  this.request_.add(request, tag);
  this.tags_.push(tag);
  return this;
};

quikos.prototype.deleteData = function(key, tag) {
  this.ensureDataRequest_();
  var request = this.request_.newRemovePersonAppDataRequest(quikos.id.VIEWER,
      opt_key);
  this.request_.add(request);
  this.tags_.push(tag);
  return this;
};
